package com.example.child_protection.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.child_protection.dao.SystemRoleButtonDao;
import com.example.child_protection.dao.SystemRoleMenuDao;
import com.example.child_protection.dto.base.BaseResponseDTO;
import com.example.child_protection.dto.info.SystemRoleButtonDTO;
import com.example.child_protection.dto.info.SystemRoleMenuDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.find.SystemRoleMenuRequestDTO;
import com.example.child_protection.dto.response.SystemRoleMenuListResponseDTO;
import com.example.child_protection.dto.response.SystemRoleMenuResponseDTO;
import com.example.child_protection.enums.BooleanEnums;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.model.SystemRoleButtonModel;
import com.example.child_protection.model.SystemRoleMenuModel;
import com.example.child_protection.service.SystemRoleMenuQueryService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色菜单信息查询service实现层
 *
 * @author Biz
 * @date 2018-11-2
 */
@Component
public class SystemRoleMenuQueryServiceImpl extends MyBaseService implements SystemRoleMenuQueryService {

    @Autowired
    public SystemRoleMenuDao systemRoleMenuDao;

    @Autowired
    public SystemRoleButtonDao systemRoleButtonDao;

    /**
     * id查询
     *
     * @param requestDTO
     * @return
     */
    public SystemRoleMenuResponseDTO findId(SimpleIdRequestDTO requestDTO) {

        return this.service(ServiceActionEnums.SYSTEM_ROLE_MENU_FIND_ID, requestDTO, SystemRoleMenuResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, SystemRoleMenuResponseDTO>() {
            @Override
            protected SystemRoleMenuResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                SystemRoleMenuResponseDTO responseDTO = new SystemRoleMenuResponseDTO();
                SystemRoleMenuModel systemMenuModel = systemRoleMenuDao.findId(requestDTO.getId());
                SystemRoleMenuDTO systemMenu = new SystemRoleMenuDTO();
                BeanUtils.copyProperties(systemMenuModel, systemMenu);
                responseDTO.setInfo(systemMenu);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }

    /**
     * 列表查询(包括按钮一起查询)
     *
     * @param requestDTO
     * @return
     */
    public SystemRoleMenuListResponseDTO findList(SystemRoleMenuRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.SYSTEM_ROLE_MENU_FIND_LIST, requestDTO, SystemRoleMenuListResponseDTO.class, new MyServiceExecutor<SystemRoleMenuRequestDTO, SystemRoleMenuListResponseDTO>() {
            @Override
            protected SystemRoleMenuListResponseDTO myprocess(SystemRoleMenuRequestDTO requestDTO) throws Exception {
                SystemRoleMenuListResponseDTO responseDTO = new SystemRoleMenuListResponseDTO();
                SystemRoleMenuModel req = new SystemRoleMenuModel();
                BeanUtils.copyProperties(requestDTO, req);
                List<SystemRoleMenuModel> lists = systemRoleMenuDao.findList(req);
                List<SystemRoleMenuDTO> menuInfos = JSONObject.parseArray(JSONArray.toJSONString(lists), SystemRoleMenuDTO.class);
                responseDTO.setInfos(menuInfos);

                SystemRoleButtonModel req1 = new SystemRoleButtonModel();
                BeanUtils.copyProperties(requestDTO, req1);
                List<SystemRoleButtonModel> lists1 = systemRoleButtonDao.findPage(req1);

                List<SystemRoleButtonDTO> buttonInfos = JSONObject.parseArray(JSONArray.toJSONString(lists1), SystemRoleButtonDTO.class);
                responseDTO.setInfos1(buttonInfos);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }
}
